package com.example.gustavo.hello.utils;

import android.content.Context;
import android.text.format.Formatter;

import org.apache.http.util.EncodingUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileUtils {
	/**
	 * 将res/raw下的数据库复制到手机
	 * @param context
	 * @param path
	 *            复制到的文件夹路径 eg:"data/data/com.example/databases"
	 * @param name
	 *            数据库文件名 eg:"test.db"
	 * @param resID		数据库id
	 * @throws java.io.IOException
	 */
	public static void copy(Context context,String path, String name,int resID) throws IOException {
			File fileDir = new File(path);
			if (!fileDir.exists()) {
				fileDir.mkdirs();
			}
			File file = new File(fileDir, name);
			if (!file.exists()) {
				file.createNewFile();
				InputStream inputStream = context.getResources().openRawResource(resID);
				FileOutputStream fos = new FileOutputStream(file);
				byte[] bs = new byte[1024];
				int len = 0;
				while ((len = inputStream.read(bs)) != -1) {
					fos.write(bs, 0, len);
				}
				fos.flush();
				inputStream.close();
				fos.close();
			}
	}
	/**
	 * 删除sd卡中某个目录
	 * @param root 目录地址
	 * @return
	 */
	public static boolean deleteAllFiles(File root) {  
        File files[] = root.listFiles();  
        if (files != null) {
        	for (File f : files) {  
        		if (f.isDirectory()) { // 判断是否为文件夹  
                    deleteAllFiles(f);  //递归
                    try {  
                        f.delete();  
                    } catch (Exception e) {
                    	e.printStackTrace();
                    	return false;
                    }  
                } else {  
                    if (f.exists()) { // 判断是否存在  
                        deleteAllFiles(f);  
                        try {  
                            f.delete();  
                        } catch (Exception e) {  
                        	e.printStackTrace();
                        	return false;
                        }  
                    }  
                }  
            }  
        }
        return true;
    }  
	
	/**
	 * 得到文件大小
	 * @param f
	 * @return
	 * @throws Exception
	 */
	public static long getFileSizes(File f) throws Exception {
		long s = 0;
		if (f.exists()) {
			FileInputStream fis = null;
			fis = new FileInputStream(f);
			s = fis.available();
		} else {
			f.createNewFile();
		}
		return s;
	}

	/**
	 * 得到目录大小
	 * @param f
	 * @return
	 * @throws Exception
	 */
	public static long getDirSize(File f) throws Exception {
		long size = 0;
		File flist[] = f.listFiles();
		for (int i = 0; i < flist.length; i++) {
			if (flist[i].isDirectory()) {
				size = size + getDirSize(flist[i]);
			} else {
				size = size + flist[i].length();
			}
		}
		return size;
	}

	/**
	 * 转换文件大小 格式化显示
	 * @param fileS
	 * @return
	 */
	public static String FormetFileSize(Context context,long fileS) {// 转换文件大小
//		DecimalFormat df = new DecimalFormat("#.00");
//		String fileSizeString = "";
//		if (fileS < 1024) {
//			fileSizeString = df.format((double) fileS) + "B";
//		} else if (fileS < 1048576) {
//			fileSizeString = df.format((double) fileS / 1024) + "K";
//		} else if (fileS < 1073741824) {
//			fileSizeString = df.format((double) fileS / 1048576) + "M";
//		} else {
//			fileSizeString = df.format((double) fileS / 1073741824) + "G";
//		}
//		return fileSizeString;
		return Formatter.formatFileSize(context, fileS);
	}

	/**
	 * 获取目录里面文件个数
	 * @param f
	 * @return
	 */
	public static long getlist(File f) {// 递归求取目录文件个数
		long size = 0;
		File flist[] = f.listFiles();
		size = flist.length;
		for (int i = 0; i < flist.length; i++) {
			if (flist[i].isDirectory()) {
				size = size + getlist(flist[i]);
				size--;
			}
		}
		return size;
	}

	public static String getFromAssets(String fileName, Context context) {
		String result = "";
		try {
			InputStream in = context.getResources().getAssets().open(fileName);
			int lenght = in.available();
			byte[] buffer = new byte[lenght];
			in.read(buffer);
			result = EncodingUtils.getString(buffer, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
